package com.limingru.online_class.service.impl;

import com.limingru.online_class.common.ServiceResultEnum;
import com.limingru.online_class.common.exception.CommonException;
import com.limingru.online_class.domain.*;
import com.limingru.online_class.mapper.*;
import com.limingru.online_class.service.VideoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional
@Service
public class VideoOrderServiceImpl implements VideoOrderService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    VideoMapper videoMapper;

    @Autowired
    VideoOrderMapper videoOrderMapper;

    @Autowired
    EpisodeMapper episodeMapper;

    @Autowired
    PlayRecordMapper playRecordMapper;

    @Override
    public int save(Integer videoId, Integer userId) {
        if (checkHasBuy(videoId, userId)) {
            throw new CommonException(500, "该视频已在订单中");
        }

        User user = userMapper.findUserById(userId);
        Video video = videoMapper.findVideoByVideoId(videoId);
        VideoOrder videoOrder = generateOrder(user, video);
        if (videoOrder != null) {
            int rows = videoOrderMapper.save(videoOrder);

            if (rows == 1) {
                Episode episode = episodeMapper.findEpisodeByVideoId(videoId);
                PlayRecord playRecord = new PlayRecord();
                playRecord.setUserId(userId);
                playRecord.setVideoId(videoId);
                playRecord.setEpisodeId(episode.getId());
                playRecord.setCurrentNum(episode.getNum());
                playRecord.setCreateTime(new Date());

                return playRecordMapper.savePlayRecord(playRecord);
            }
            return rows;
        }
        return -1;
    }

    @Override
    public List<VideoOrder> listOrders(Integer userId) {
        return videoOrderMapper.findOrdersByUserId(userId);
    }

    @Override
    public String removeOrder(Integer orderId, Integer userId) {
        int i = videoOrderMapper.removeOrderByIdAndUserId(orderId, userId);
        return i > 0
                ? ServiceResultEnum.SUCCESS.getText()
                : ServiceResultEnum.FAIL.getText();
    }

    /**
     * 判断是否已经购买过该视频
     */
    private boolean checkHasBuy(Integer videoId, Integer userId) {
        VideoOrder videoOrder = videoOrderMapper.findOrderByVideoIdAndUserId(videoId, userId);
        return videoOrder != null;
    }

    /**
     * 生成一个 VideoOrder
     */
    private VideoOrder generateOrder(User user, Video video) {
        if (user == null || video == null) {
            return null;
        }
        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setUserId(user.getId());
        videoOrder.setVideoId(video.getId());
        videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setVideoImg(video.getCoverImg());
        videoOrder.setTotalFee(video.getPrice());
        videoOrder.setCreateTime(new Date());
        videoOrder.setState(0);
        videoOrder.setOutTradeNo(UUID.randomUUID().toString());
        return videoOrder;
    }
}
